﻿/*======================================*\
* Author: Vincent
* LastModifyUser: Vincent
* Description:  
* CreateTime： 2012-05
* Remark:  
\*======================================*/
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using AppCode.Util;
using AppCode.Domain.Services;
using AppCode.Contract;
using AppCode.Config;
using AppCode.Mapping;

namespace Vincent.AutoCode 
{
    public partial class frmMain : Form
    {
        /// <summary>
        /// 数据初始化
        /// </summary>
        ILoadDbInfoInit dbHandler = new DbInfoInit();

        public frmMain()
        {
            InitializeComponent();
            this.MaximizeBox = false;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
          
        }

        #region OnInit

        /// <summary>
        /// 窗体初始化加载表信息
        /// </summary>
        void FrmOnInit()
        {
            dbHandler.LoadTreeInfoEvent += new EventHandler<TaskInfoEventArgs>(dbHandler_LoadTreeInfo);
            dbHandler.ReportStatusMsgEvent += new EventHandler<TaskInfoEventArgs>(dbHandler_ReportMsgEvent);
            dbHandler.ReportTaskProcessEvent += new EventHandler<TaskInfoProcessEventArgs>(dbHandler_ReportTaskProcessEvent);
            dbHandler.ReportTaskInfoToTextEvent += new EventHandler<TaskInfoEventArgs>(dbHandler_ReportTaskInfoToTextEvent);
            dbHandler.BeginCreateCodeFile += new EventHandler(dbHandler_BeginCreateCodeFile);
          
            dbHandler.DbOnInit();  //使用默认路径加载配置
            TreeInfoOnInit();// 初始化数据树
            btnExcute.Enabled = true;//允许执行生成
        }


        #endregion

        /// <summary>
        /// 开始生成类代码到磁盘
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExcute_Click(object sender, EventArgs e)
        {
            GetSelectTable();
            if (string.IsNullOrEmpty(txtNameSpace.Text.Trim()))
            {
                MessageBox.Show("请输入命名空间！", "提 示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else if (currentSelectTable == null || currentSelectTable.Count < 1)
            {
                MessageBox.Show("请选择要操作的数据表！", "提 示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (MessageBox.Show("代码文件将生成至:"+txtOutPutFolder.Text.Trim()+",确认此操作？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) != DialogResult.Yes)
            {
                return;
            }
           
          //  btnExcute.Enabled = false;
            btnDataInit.Enabled = false;
            
            dbHandler.OnReadTableColumnInfo(currentSelectTable, 10);
            currentSelectTable = null;
        }

        /// <summary>
        /// 数据初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDataInit_Click(object sender, EventArgs e)
        {
            btnDataInit.Enabled = false;
            string defaultConfig = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "ConfigXml\\DbConfig.xml";
            if (System.IO.File.Exists(defaultConfig))
            {
                rtbLog.Clear();
                FrmOnInit();
            }
            else
            {
                frmDbConfig cfg = new frmDbConfig();
                cfg.ShowDialog();
                btnDataInit.Enabled = true;
            }
        }

        /// <summary>
        /// 浏览文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBrowerFolder_Click(object sender, EventArgs e)
        {
            FolderDialog f = new FolderDialog();
            if (f.DisplayDialog() != DialogResult.OK)
            {
                return;
            }
            txtOutPutFolder.Text = f.Path+"\\";
        }

        private void btnParamClass_Click(object sender, EventArgs e)
        {
            ParamClassHandler hd = new ParamClassHandler();
           this.txtResult.Text = hd.CreateGridCode(txtConnStr.Text, txtSql.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

 



 
    }
}
